package org.example.user.controller;

import lombok.RequiredArgsConstructor;
import org.example.api.DeptServiceApi;
import org.example.vo.DeptVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


/**
 * @author 天天向上 （john.yi@qq.com）
 * @data 2020/8/31.
 */
@RestController
@RequestMapping("/users")
@RefreshScope
@RequiredArgsConstructor
public class UserController {
    private final DeptServiceApi deptServiceApi;
    private final LoadBalancerClient loadBalancerClient;
    private final RestTemplate restTemplate;
    @Value("${dept-service.url}")
    private String deptServiceUrl;
    @Value("${server.port}")
    private String port;
    @Value("${user.name}")
    private String userName;

    @GetMapping("/choose")
    public Object choose() {
        return loadBalancerClient.choose("dept-service");
    }
    @GetMapping("/hello")
    public String hello(String username) {
        String deptName = restTemplate.getForObject(deptServiceUrl + "/depts/1", String.class);
        return "hello " + username + deptName;
    }

    @GetMapping("/hello2")
    public String hello2(String username) {
        ResponseEntity<String> forEntity = restTemplate.getForEntity("http://dept-service/depts/1", String.class);
        System.out.println(forEntity.getStatusCode().value());
        System.out.println(forEntity.getStatusCodeValue());
        System.out.println(forEntity.getBody());
        String deptName = restTemplate.getForObject("http://dept-service/depts/1", String.class);
        return "hello " + username + deptName + ":" + port;
    }

    @GetMapping("/hello3/{deptId}")
    public Object hello3(@PathVariable Integer deptId) {

        DeptVo dept = restTemplate.getForObject("http://dept-service/depts/obj/"+ deptId, DeptVo.class);
        System.out.println(dept);
        ResponseEntity<DeptVo> forEntity = restTemplate.getForEntity("http://dept-service/depts/obj/"+deptId, DeptVo.class);
        System.out.println(forEntity.getStatusCodeValue());
        System.out.println(forEntity.getBody().getId() + "," + forEntity.getBody().getDeptName());
        return dept;
    }

    @GetMapping("/feign/{id}")
    public Object feignId(@PathVariable Integer id) {
        String name = deptServiceApi.byId(id);
        return name;
    }

    @GetMapping("/feign/obj/{id}")
    public Object feignObjId(@PathVariable Integer id) {
        DeptVo deptVo = deptServiceApi.byId2(id);
        return deptVo;
    }

    @GetMapping("/name")
    public String name() {
        return this.userName;
    }
}
